package com.turing.model.caller.service.impl;

import com.turing.common.annotations.RLockAnnotation;
import com.turing.common.response.ResponseVO;
import com.turing.common.util.StringUtils;
import com.turing.model.caller.client.task.manager.DrawClient;
import com.turing.model.caller.service.DrawService;
import com.turing.model.caller.utils.SpringUtil;
import com.turing.model.caller.vo.in.AnalysisResultVo;
import com.turing.model.caller.vo.in.FileTaskVo;
import com.turing.model.caller.vo.in.OcrPageTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

@Service
public class DrawServiceImpl implements DrawService {

    private static final Logger logger = LoggerFactory.getLogger(DrawServiceImpl.class);

    @Autowired
    private DrawClient drawClient;

    @Override
    //@RLockAnnotation(lockKey = "getAndLockOcrPageTask")
    public ResponseVO<OcrPageTask> getAndLockOcrPageTask(String url,String client){
        Method method = SpringUtil.getMethod(client, "getAndLockOcrPageTask",String.class);
        return (ResponseVO<OcrPageTask>)ReflectionUtils.invokeMethod(method,SpringUtil.getBean(client),url);
    }

    @Override
    @RLockAnnotation(lockKey = "getAndLockForecastTask")
    public ResponseVO<FileTaskVo> getAndLockForecastTask(String url, String templates) {
        if (StringUtils.isEmpty(templates)) {
            logger.info("Forecast拉取任务失败，场景信息为空 url：" + url + " templates:" + templates);
            return null;
        }
        return drawClient.getAndLockForecastTask(url, templates);
    }

    @Override
    @RLockAnnotation(lockKey = "getAndLockAnalysisTask")
    public ResponseVO<FileTaskVo> getAndLockAnalysisTask(String url,String client) {
        Method method = SpringUtil.getMethod(client, "getAndLockAnalysisTask",String.class);
        return (ResponseVO<FileTaskVo>)ReflectionUtils.invokeMethod(method,SpringUtil.getBean(client),url);
    }

    @Override
    public ResponseVO<Object> unLockOcrPageTask(String ocrPageTaskSn,String client) {
        Method method = SpringUtil.getMethod(client, "unLockOcrPageTask",String.class);
        return (ResponseVO<Object>)ReflectionUtils.invokeMethod(method,SpringUtil.getBean(client),ocrPageTaskSn);
    }

    @Override
    public ResponseVO<Object> unLockAnalysisTask(String ocrPageTaskSn, String client) {
        Method method = SpringUtil.getMethod(client, "unLockAnalysisTask",String.class);
        return (ResponseVO<Object>)ReflectionUtils.invokeMethod(method,SpringUtil.getBean(client),ocrPageTaskSn);
    }

    @Override
    public ResponseVO<Object> analysisCallback(AnalysisResultVo analysisResultVo, String client) {
        Method method = SpringUtil.getMethod(client, "analysisCallback",AnalysisResultVo.class);
        return (ResponseVO<Object>)ReflectionUtils.invokeMethod(method,SpringUtil.getBean(client),analysisResultVo);
    }

    @Override
    public ResponseVO<FileTaskVo> getAndLockCurriculumVitaeTask(String url) {
        return drawClient.getAndLockCurriculumVitaeTask(url);
    }

    @Override
    public ResponseVO<Object> unLockCurriculumVitaeTask(String taskSn) {
        return drawClient.unLockAnalysisTask(taskSn);
    }

    @Override
    public ResponseVO<Object> curriculumVitaeCallback(AnalysisResultVo analysisResultVo) {
        return drawClient.curriculumVitaeCallback(analysisResultVo);
    }

}

